<template>
  <div>
    <v-row>
      <v-col cols="12" md="2">
        <v-btn color="#2196f3" dark @click="dialogFormVisible = true">新增轮播图</v-btn>
      </v-col>
    </v-row>
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      :data="carouselList"
      border
      style="width: 100%"
    >
      <el-table-column fixed prop="title" label="标题" width="300"></el-table-column>
      <el-table-column prop="img" label="图片" width="300">
        <template slot-scope="scope">
          <el-image style="width: 200px; height: 100px" :src="scope.row.img | toCarouselUrl"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="链接" width="300"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button @click="deleteCarousel(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="新增轮播图" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            v-loading="imgUploading"
            class="center"
            name="img"
            :action="actionImg"
            :headers="headers"
            :show-file-list="false"
            :before-upload="beforeUploadImg"
            :on-success="successImg"
            drag
            accept=".jpg, .jpeg, .png"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将图片拖到此处，或
              <em>点击上传</em>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="链接(选填)">
          <el-input v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <v-btn color="#fff" tile @click="dialogFormVisible = false">取 消</v-btn>
        <v-btn color="#2196f3" dark @click="beforeaddCarousel('form')">确 定</v-btn>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCarouselList,
  addCarousel,
  deleteCarousel,
} from "@/api/adminSerivce";
import { uploadCarouse, toCarouselUrl } from "@/utils/url";
import storageService from "@/utils/storageService";
export default {
  data() {
    return {
      carouselList: [],
      dialogFormVisible: false,
      imgUploading: false,
      loading: true,
      form: {
        title: "",
        img: "",
        url: "",
      },
      actionImg: uploadCarouse,
      headers: {
        Authorization:
          "Bearer " + storageService.get(storageService.ADMIN_TOKEN),
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
    };
  },
  methods: {
    _getCarouselList() {
      getCarouselList().then((res) => {
        if (res.data.code === 200) {
          this.carouselList = res.data.data.data;
          this.loading = false;
        }
      });
    },
    beforeUploadImg(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const img1 = testmsg === "jpg";
      const img2 = testmsg === "jpeg";
      const img3 = testmsg === "png";
      const isLt1M = file.size / 1024 / 1024 < 3;
      if (!img1 && !img2 && !img3) {
        this.$message.error("格式只能是jpg/jpeg/png格式!");
        return false;
      }
      if (!isLt1M) {
        this.$message.error("图片大小不能超过 3MB!");
        return false;
      }
      this.imgUploading = true;
      return true;
    },
    successImg(res) {
      this.form.img = res.data.img;
      this.imgUploading = false;
      this.$message.success("上传成功");
    },
    beforeaddCarousel(addForm) {
      this.$refs[addForm].validate((valid) => {
        if (valid) {
          this.addCarousel();
        } else {
          this.$message.error("请检查输入的数据");
          return;
        }
      });
    },
    addCarousel() {
      this.dialogFormVisible = false;
      addCarousel(this.form)
        .then((res) => {
          if (res.data.code === 200) {
            this.$message.success("发送成功");
            //重新获取
            this._getCarouselList();
          }
        })
        .catch((err) => {
          this.$message.error(err.response.data.msg);
        });
      this.form.title = "";
      this.form.img = "";
      this.form.url = "";
    },
    deleteCarousel(row) {
      deleteCarousel(row.id).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("删除成功");
          //重新获取
          this._getCarouselList();
        }
      });
    },
  },
  created() {
    this._getCarouselList();
  },
  filters: {
    toCarouselUrl(name) {
      return toCarouselUrl(name);
    },
  },
};
</script>